package com.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.model.Project;
import com.demo.service.ProjectService;

@Controller
public class ProjectController {

	@Autowired
	private ProjectService projectService;

	@RequestMapping("/project/list.htm")
	public String list(String name, Model model) {
		List<Project> projects = projectService.getProjects(name);
		model.addAttribute("projects", projects);
		model.addAttribute("name", name);
		return "project/list";
	}

	@RequestMapping("/project/input.htm")
	public String input(@RequestParam(value = "id", required = false, defaultValue = "0") int id, Model model) {
		if (id > 0) {
			Project project = projectService.getProjectById(id);
			model.addAttribute("project", project);
		}
		return "project/input";
	}

	@RequestMapping("/project/update.htm")
	public String update(Project project) {
		projectService.updateProject(project);
		return "redirect:/project/list.htm";
	}

	@RequestMapping("/project/delete.htm")
	public String delete(@RequestParam(value = "id", required = false, defaultValue = "0") int id) {
		projectService.deleteProject(id);
		return "redirect:/project/list.htm";
	}

}
